package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.ResultConstants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.system.domain.WdyEthTxn;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.mapper.WdyEthTxnMapper;
import com.ruoyi.system.service.IWdyEthTxnService;
import com.ruoyi.system.service.IWdyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * eth交易 服务层实现
 * 
 * @author ruoyi
 * @date 2020-02-25
 */
@Service
public class WdyEthTxnServiceImpl extends ServiceImpl<WdyEthTxnMapper, WdyEthTxn> implements IWdyEthTxnService
{
	@Autowired
	private IWdyUserService wdyUserService;
	/**
     * 查询eth交易信息
     * 
     * @param id eth交易ID
     * @return eth交易信息
     */
    @Override
	public WdyEthTxn selectWdyEthTxnById(Long id)
	{
	    return baseMapper.selectWdyEthTxnById(id);
	}
	
	/**
     * 查询eth交易列表
     * 
     * @param wdyEthTxn eth交易信息
     * @return eth交易集合
     */
	@Override
	public List<WdyEthTxn> selectWdyEthTxnList(WdyEthTxn wdyEthTxn)
	{
	    return baseMapper.selectWdyEthTxnList(wdyEthTxn);
	}
	
    /**
     * 新增eth交易
     * 
     * @param wdyEthTxn eth交易信息
     * @return 结果
     */
	@Override
	public int insertWdyEthTxn(WdyEthTxn wdyEthTxn)
	{
	    return baseMapper.insertWdyEthTxn(wdyEthTxn);
	}
	
	/**
     * 修改eth交易
     * 
     * @param wdyEthTxn eth交易信息
     * @return 结果
     */
	@Override
	public int updateWdyEthTxn(WdyEthTxn wdyEthTxn)
	{
	    return baseMapper.updateWdyEthTxn(wdyEthTxn);
	}

	/**
     * 删除eth交易对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteWdyEthTxnByIds(String ids)
	{
		return baseMapper.deleteWdyEthTxnByIds(Convert.toStrArray(ids));
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void handleTxn(WdyEthTxn t) {
		QueryWrapper<WdyUser> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", t.getUserId());
		WdyUser user = wdyUserService.getOne(queryWrapper);

		WdyUser updateUser = new WdyUser();
		updateUser.setId(user.getId());
		if(t.getTxnCurrency().intValue() == WdyEthTxn.ETH) {
			updateUser.setEthAmount(BigDecimalUtil.add(user.getEthAmount(), t.getTxnValue()));
		} else if(t.getTxnCurrency().intValue() == WdyEthTxn.USDT) {
			updateUser.setUsdtAmount(BigDecimalUtil.add(user.getUsdtAmount(), t.getTxnValue()));
		}
		updateUser.setVersion(user.getVersion());
		if (!wdyUserService.updateById(updateUser)) {
			throw new BusinessException(ResultConstants.EX9999, "系统繁忙，请稍后重试");
		}

		UpdateWrapper<WdyEthTxn> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", t.getId());
		updateWrapper.eq("txn_state", WdyEthTxn.PROCESSING);
		updateWrapper.set("txn_state", WdyEthTxn.SUCCESS);
		if(!this.update(updateWrapper)) {
			throw new BusinessException(ResultConstants.EX9999, "系统繁忙，请稍后重试");
		}
	}

}
